<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="缴纳人" prop="userId">
      <el-select v-model="dataForm.userId" placeholder="用户">
        <el-option v-for="user in userList" :key="user.userId" :label="user.username" :value="user.userId" />
      </el-select>
    </el-form-item>
    <el-form-item label="缴纳金额" prop="paymentMoney">
      <el-input v-model="dataForm.paymentMoney" placeholder="缴纳金额"></el-input>
    </el-form-item>
    <el-form-item label="缴纳时间" prop="paymentTime">
      <el-date-picker v-model="dataForm.paymentTime" placeholder="缴纳时间" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" />
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          paymentMoney: '',
          paymentTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '缴纳人id不能为空', trigger: 'blur' }
          ],
          paymentMoney: [
            { required: true, message: '缴纳金额不能为空', trigger: 'blur' },
            { pattern: /^[1-9]\d*$/, message: '缴费金额必须是正整数', trigger: 'blur' }
          ],
          paymentTime: [
            { required: true, message: '缴纳时间不能为空', trigger: 'blur' }
          ]
        },
        userList: []
      }
    },
    methods: {
      // 获取角色为机动车车主的用户
      getUserList () {
        this.$http({
          url: this.$http.adornUrl('/mvs/owner/list'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.userList = data.userList
          } else {
            this.userList = []
          }
        })
      },
      init (id) {
        this.getUserList()
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/mvs/payment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.payment.userId
                this.dataForm.paymentMoney = data.payment.paymentMoney
                this.dataForm.paymentTime = data.payment.paymentTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mvs/payment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: !this.dataForm.id ? 'post' : 'put',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'paymentMoney': this.dataForm.paymentMoney,
                'paymentTime': this.dataForm.paymentTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
